package zyd.springboot.springbootmybatis.controller;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import zyd.springboot.springbootmybatis.service.ex.LoginException;
import zyd.springboot.springbootmybatis.service.ex.ServiceException;
import zyd.springboot.springbootmybatis.service.ex.UsernameDuplicateException;

import java.sql.SQLException;

@ControllerAdvice
public class GlobalExceptionHandlerController {
	@ExceptionHandler(value= ServiceException.class)
	@ResponseBody
	public String handlerException(ServiceException e) {
		//数据库异常
		if (e instanceof LoginException) {
			System.out.println("全局统一异常处理: "+e.getMessage());//LoginException
			return e.getMessage();
		} else if (e instanceof UsernameDuplicateException) {
			System.out.println("全局统一异常处理："+e.getMessage());//UsernameDuplicateException
			return e.getMessage();
		} else {//未知异常
			System.out.println("全局统一异常处理："+e.getMessage());//InsertExceptionl
			return e.getMessage();
		}
	}
}
